/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.templates.client.bindings;

import org.nuxeo.gwt.habyt.templates.client.WidgetFactory;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public abstract class ComplexWidgetFactory extends WidgetFactory {

    public ComplexWidgetFactory(String id) {
        super(id);
    }

    protected SimplePanel createCell(Element element) {
        SimplePanel cell = new SimplePanel();
        // this is required to span the div on all the container area
        cell.setSize("100%", "100%");
        Element cellElement = cell.getElement();
        String id = Helper.getIdAttribute(element);
        if (id != null) {
            cellElement.setId(id);
        }
        String className = Helper.getClassAttribute(element);
        if (className != null) {
            cellElement.addClassName(className);
        }
        String style = Helper.getStyleAttribute(element);
        if (style != null) {
            Helper.setSyleAttribute(cellElement, style);
        }
        Node el = element.getFirstChild();
        while (el != null) {
            Node next = el.getNextSibling();
            cellElement.appendChild(el);
            el = next;
        }
        return cell;
    }

}
